<template>
    <el-dialog
            :title="!dataForm.customerId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="客户编号，主键" prop="customerId">
                        <el-input v-model.number="dataForm.customerId" placeholder="客户编号，主键"></el-input>
                </el-form-item>
                <el-form-item label="客户姓名" prop="name">
                        <el-input v-model="dataForm.name" placeholder="客户姓名"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="gender">
                        <el-input v-model="dataForm.gender" placeholder="性别"></el-input>
                </el-form-item>
                <el-form-item label="年龄" prop="age">
                        <el-input v-model.number="dataForm.age" placeholder="年龄"></el-input>
                </el-form-item>
                <el-form-item label="身份证号，唯一" prop="idCard">
                        <el-input v-model="dataForm.idCard" placeholder="身份证号，唯一"></el-input>
                </el-form-item>
                <el-form-item label="联系方式" prop="contact">
                        <el-input v-model="dataForm.contact" placeholder="联系方式"></el-input>
                </el-form-item>
                <el-form-item label="家庭住址" prop="address">
                        <el-input v-model="dataForm.address" placeholder="家庭住址"></el-input>
                </el-form-item>
                <el-form-item label="职业" prop="occupation">
                        <el-input v-model="dataForm.occupation" placeholder="职业"></el-input>
                </el-form-item>
                <el-form-item label="年收入" prop="annualIncome">
                        <el-input v-model="dataForm.annualIncome" placeholder="年收入"></el-input>
                </el-form-item>
                <el-form-item label="配偶基本信息" prop="spouseInfo">
                        <el-input v-model="dataForm.spouseInfo" placeholder="配偶基本信息"></el-input>
                </el-form-item>
                <el-form-item label="资产状况" prop="assetStatus">
                        <el-input v-model="dataForm.assetStatus" placeholder="资产状况"></el-input>
                </el-form-item>
                <el-form-item label="负债情况" prop="debtStatus">
                        <el-input v-model="dataForm.debtStatus" placeholder="负债情况"></el-input>
                </el-form-item>
                <el-form-item label="子女等家庭成员基本信息" prop="childrenInfo">
                        <el-input v-model="dataForm.childrenInfo" placeholder="子女等家庭成员基本信息"></el-input>
                </el-form-item>
                <el-form-item label="既往病史" prop="medicalHistory">
                        <el-input v-model="dataForm.medicalHistory" placeholder="既往病史"></el-input>
                </el-form-item>
                <el-form-item label="家族病史" prop="familyMedicalHistory">
                        <el-input v-model="dataForm.familyMedicalHistory" placeholder="家族病史"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            customerId: null,
            name: null,
            gender: null,
            age: null,
            idCard: null,
            contact: null,
            address: null,
            occupation: null,
            annualIncome: null,
            spouseInfo: null,
            assetStatus: null,
            debtStatus: null,
            childrenInfo: null,
            medicalHistory: null,
            familyMedicalHistory: null
          },
          dataRule: {
            customerId: [
                        { required: true, message: '客户编号，主键不能为空', trigger: 'blur' }
            ],
            name: [
                        { required: true, message: '客户姓名不能为空', trigger: 'blur' }
            ],
            gender: [
                        { required: true, message: '性别不能为空', trigger: 'blur' }
            ],
            age: [
                        { required: true, message: '年龄不能为空', trigger: 'blur' }
            ],
            idCard: [
                        { required: true, message: '身份证号，唯一不能为空', trigger: 'blur' }
            ],
            contact: [
                        { required: true, message: '联系方式不能为空', trigger: 'blur' }
            ],
            address: [
                        { required: true, message: '家庭住址不能为空', trigger: 'blur' }
            ],
            occupation: [
                        { required: true, message: '职业不能为空', trigger: 'blur' }
            ],
            annualIncome: [
                        { required: true, message: '年收入不能为空', trigger: 'blur' }
            ],
            spouseInfo: [
                        { required: true, message: '配偶基本信息不能为空', trigger: 'blur' }
            ],
            assetStatus: [
                        { required: true, message: '资产状况不能为空', trigger: 'blur' }
            ],
            debtStatus: [
                        { required: true, message: '负债情况不能为空', trigger: 'blur' }
            ],
            childrenInfo: [
                        { required: true, message: '子女等家庭成员基本信息不能为空', trigger: 'blur' }
            ],
            medicalHistory: [
                        { required: true, message: '既往病史不能为空', trigger: 'blur' }
            ],
            familyMedicalHistory: [
                        { required: true, message: '家族病史不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.customerId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.customerId = id || 0
            if (this.dataForm.customerId) {
              this.$http({
                url: this.$http.adornUrl(`/app/customer_info/info/${this.dataForm.customerId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appcustomerinfo
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/customer_info/${!this.dataForm.customerId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>